import { Toast } from "vant";
import { Image as VanImage } from "vant";
import cTitle from "components/title";
export default {
  components: {
    VanImage,
    cTitle
  },
  data() {
    return {
      storeGoodsList: [],
      searchStoreList: [],
      storeName: "",
      longitude: "",
      latitude: "",
      goodsPage: 1,
      goodsLastPage: 0,
      goodsMore: false,
      storePage: 1,
      storeLastPage: 0,
      storeMore: false,
      locationStatus: false
    };
  },
  activated() {
    if ((this.$route.query.keyword ?? "") !== "") {
      this.storeName = this.$route.query.keyword.trim();
    }
    let arr = localStorage.getItem("searchHistory") || [];
    let searchArr = [];
    if (arr.length > 0) {
      arr = JSON.parse(arr);
      let len = arr.length;
      if (len >= 10) {
        arr.pop();
      }
    }
    let timer = Date.parse(new Date());
    arr.unshift({ name: this.storeName, key: timer });
    searchArr = arr.filter((currentValue, currentIndex, selfArr) => {
      //去重
      return selfArr.findIndex(x => x.name === currentValue.name) === currentIndex;
    });
    localStorage.setItem("searchHistory", JSON.stringify(searchArr));
    this.fun
      .getLocation()
      .then(res => {
        let p = this.fun.bd_encrypt(res.point.lng, res.point.lat);
        this.latitude = p.lng;
        this.longitude = p.lat;
        this.locationStatus = true;
        this.searchStore();
      })
      .catch(err => {
        console.log(err);
        this.locationStatus = false;
      });
    this.searchGoods();
  },
  filters: {
    caitalize(value) {
      let v;
      if (typeof value === "number" && !isNaN(value)) {
        if (value >= 1000) {
          v = (value / 1000).toFixed(2) + "km";
        } else {
          v = value + "m";
        }
      } else {
        v = "0m";
      }
      return v;
    }
  },
  methods: {
    storeInput(event) {
      this.storeName = event;
    },
    commercialOn(id) {
      //跳转商户
      this.$router.push(this.fun.getUrl("commodity", {}, { storeId: id }));
    },
    goodsNavTo(id) {
      //跳转商品
      this.$router.push(this.fun.getUrl("goods", { id: id }));
    },
    searchClick() {
      if ((this.storeName ?? "") !== "") {
        this.searchStore();
        this.searchGoods();
      }
    },
    searchStore() {
      //搜索商户
      let json = {
        store_name: this.storeName,
        latitude: this.latitude,
        longitude: this.longitude,
        page: this.storePage
      };
      $http.post("plugin.coupon-store.frontend.search.searchStore", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.storeMore = true;
            this.storeLastPage = res.last_page;
            if (!this.storeLastPage) {
              this.page = 0;
            }
            this.searchStoreList = res.data;
          } else {
            this.page = this.page - 1;
            this.storeMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    searchGoods() {
      //搜索商品
      let json = {
        search: {
          keyword: this.storeName
        },
        page: this.goodsPage
      };
      $http.post("plugin.coupon-store.frontend.search.searchGoods", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.totalPage = res.last_page;
            if (!this.totalPage) {
              this.page = 0;
            }
            this.storeGoodsList = res.data;
            this.goodsMore = true;
          } else {
            this.page = this.page - 1;
            this.storeMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    goodsMoreData() {
      this.goodsMore = false; // 防止多次请求分页数据
      if (this.goodsPage >= this.goodsLastPage) {
        return;
      } else {
        this.goodsPage = this.goodsPage + 1;
        let json = {
          search: {
            keyword: this.storeName
          },
          page: this.goodsPage,
          paginate: 5
        };
        $http.post("plugin.coupon-store.frontend.search.searchGoods", json, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.goodsMore = true;
              this.goodsLastPage = res.last_page;
              this.storeGoodsList = [...this.storeGoodsList, ...list];
            } else {
              this.page = this.page - 1;
              this.storeMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    },
    storeMoreData() {
      this.storeMore = false; // 防止多次请求分页数据
      if (this.storePage >= this.storeLastPage) {
        return;
      } else {
        this.storePage = this.storePage + 1;
        let json = {
          search: {
            keyword: this.storeName
          },
          page: this.storePage
        };
        $http.post("plugin.coupon-store.frontend.search.searchStore", json, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.storeMore = true;
              this.storeLastPage = res.last_page;
              this.searchStoreList = [...this.searchStoreList, ...list];
            } else {
              this.page = this.page - 1;
              this.storeMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    }
  }
};
